﻿using System;
using System.Xml;

namespace BuzzX8.Xmpp.DataForms
{
    public class Field : StreamElement
    {
        private OptionCollection options;

        public Field() : base("field")
        {
            options = new OptionCollection();
        }

        public FieldType Type { get; set; }

        public string Description { get; set; }

        public string FieldId
        {
            get { return Attributes["var"] as string; }
            set { Attributes["var"] = value; }
        }

        public string FieldName
        {
            get { return Attributes["label"] as string; }
            set { Attributes["label"] = value; }
        }

        public object Value { get; set; }

        public bool Required { get; set; }

        public OptionCollection Options
        {
            get { return options; }
        }

        protected override void OnBeforeWrite()
        {
            base.OnBeforeWrite();
            Attributes["type"] = GetStringForType();
        }

        protected override void WritePayload(XmlWriter writer)
        {
            base.WritePayload(writer);
            WriteDescription(writer);
            WriteReqiredField(writer);
            WriteValue(writer);
            WriteOptions(writer);
        }

        private void WriteOptions(XmlWriter writer)
        {
            if(Options.Count > 0) Options.Write(writer);
        }

        private void WriteValue(XmlWriter writer)
        {
            if (Value != null) writer.WriteElementString("value", Value.ToString());
        }

        private void WriteDescription(XmlWriter writer)
        {
            if(!string.IsNullOrEmpty(Description)) writer.WriteElementString("desc", Description);
        }

        private void WriteReqiredField(XmlWriter writer)
        {
            if(Required)
            {
                writer.WriteElementString("required", null);
            }
        }

        private string GetStringForType()
        {
            switch (Type)
            {
                case FieldType.None:
                    return "";

                case FieldType.Boolean:
                    return "boolean";

                case FieldType.Fixed:
                    return "fixed";

                case FieldType.Hidden:
                    return "hidden";

                case FieldType.JidSingle:
                    return "jid-single";

                case FieldType.JidMulti:
                    return "jid-multi";

                case FieldType.ListSingle:
                    return "list-single";

                case FieldType.ListMulti:
                    return "list-multi";

                case FieldType.TextSingle:
                    return "text-single";

                case FieldType.TextMulti:
                    return "text-multi";

                case FieldType.TextPrivate:
                    return "text-private";

                default:
                    throw new NotImplementedException();
            }
        }
    }
}
